/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nxlv2.nomad.view.ficheClient;

import com.lowagie.text.Cell;
import com.nxlv2.nomad.accessdb.services.CommandeServices;
import com.nxlv2.nomad.accessdb.services.DemandeServices;
import com.nxlv2.nomad.accessdb.services.DetailCommandeServices;
import com.nxlv2.nomad.accessdb.services.DetailDemandeServices;
import com.nxlv2.nomad.businessobjects.Article;
import com.nxlv2.nomad.businessobjects.Client;
import com.nxlv2.nomad.businessobjects.Commande;
import com.nxlv2.nomad.businessobjects.Demande;
import com.nxlv2.nomad.businessobjects.DetailCommande;
import com.nxlv2.nomad.businessobjects.DetailDemande;
import com.nxlv2.nomad.businessobjects.EtatCommande;
import com.nxlv2.nomad.businessobjects.Prix;
import com.nxlv2.nomad.businessobjects.TypeDemande;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.joda.time.DateTime;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;
import com.nxlv2.nomad.accessdb.services.AdresseServices;
import com.nxlv2.nomad.businessobjects.Adresse;
import java.awt.Color;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nastasia
 */
public class FicheDetailDemande extends javax.swing.JFrame {

    private final Demande demande;
    private final Commande commande;
    private final Client client;
    private final FicheClient ficheClient;
    private List<DetailDemande> ListDetailDemande;
    private List<DetailCommande> ListDetailCommande;

    /**
     * Creates new form DetailDemande
     */
    public FicheDetailDemande(FicheClient ficheClient, Client client, Demande demande, Commande commande) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.client = client;
        this.demande = demande;
        this.commande = commande;
        this.ficheClient = ficheClient;
        initData();
    }

    private void initData() {
        //cache tous les boutons
        jButtonAccepter.setVisible(false);
        jButtonCommande.setVisible(false);
        jButtonDevis.setVisible(false);
        jButtonEditer.setVisible(false);
        jButtonImprimer.setVisible(false);
        jButtonInteret.setVisible(false);
        jButtonRefuser.setVisible(false);
        jButtonValider.setVisible(false);
        // Montre les boutons selon l'état de la demande
        if (null != demande) {
            String libTypeDemande = demande.getTypeDemande().getLibelle();

            switch (libTypeDemande) {
                case "Prospect":
                    boutonsProspect();
                    break;
                case "Intérêt":
                    boutonsInteret();
                    break;
                case "Devis":
                    if (0 == demande.getValideDemande()) {
                        boutonsDevis();
                    } else if (1 == demande.getValideDemande()) {
                        boutonsDevisValide();
                    }
                    break;
                case "Commande":
                    boutonsCommande();
                    break;
            }
            // Informations des label
            this.setTitle("Détail de la demande");
            jLabelNumDemande.setText("Demande D" + demande.getNumDemande());
            if (1 == demande.getValideDemande()) {
                jLabelEtatDemande.setText("Etat de la demande : " + libTypeDemande + " validé");
            } else {
                if (3 == demande.getValideDemande()) {
                    jLabelEtatDemande.setText("Etat de la demande : " + libTypeDemande + " refusé");
                } else {
                    jLabelEtatDemande.setText("Etat de la demande : " + libTypeDemande);
                }
            }
        } else {
            this.setTitle("Détail de la commande");
            jLabelNumDemande.setText("Commande C" + commande.getNumCommande());
            jLabelEtatDemande.setText("Etat de la commande : " + commande.getEtatCommande().getLibelle());
        }
        // Remplissage jTable
        majDetailDemande();
    }

    private void majDetailDemande() {
        if (null != demande) {
            List<DetailDemande> detailDemandes = new ArrayList<>();

            DemandeServices srvDem = new DemandeServices();
            DetailDemandeServices srvDet = new DetailDemandeServices();
            detailDemandes = srvDem.getDetailDemandes(demande);
            final DefaultTableModel model = (DefaultTableModel) jTableDetailDemande.getModel();
            model.getDataVector().removeAllElements();

            // {{{ COR [type_demande]
            this.ListDetailDemande = new ArrayList<>();
            for (DetailDemande detailDemande : detailDemandes) {
//            System.out.print(demande.getNumDemande());
                System.out.println(detailDemande.getTypeDemande() + " == " + demande.getTypeDemande());
                if (detailDemande.getTypeDemande() == demande.getTypeDemande()) {
                    this.ListDetailDemande.add(detailDemande);
                    Article article = srvDet.getArticle(detailDemande);
                    Prix prix = srvDet.getPrix(detailDemande);
                    Object[] obj = new Object[]{article, article.getReference(), detailDemande.getQuantite(), prix};
                    model.addRow(obj);
                }

            }
        } else {
            List<DetailCommande> detailCommandes = new ArrayList<>();

            CommandeServices srvCom = new CommandeServices();
            DetailCommandeServices srvDet = new DetailCommandeServices();
            detailCommandes = srvCom.getDetailCommandes(commande);

            final DefaultTableModel model = (DefaultTableModel) jTableDetailDemande.getModel();
            model.getDataVector().removeAllElements();

            this.ListDetailCommande = new ArrayList<>();
            for (DetailCommande detailCommande : detailCommandes) {
                this.ListDetailCommande.add(detailCommande);
                Article article = srvDet.getArticle(detailCommande);
                Prix prix = srvDet.getPrix(detailCommande);
                Object[] obj = new Object[]{article, article.getReference(), detailCommande.getQuantite(), prix};
                model.addRow(obj);
            }
        }
        // COR }}}
    }
    // Affiche les boutons pour une demande prospect

    private void boutonsProspect() {
        jButtonEditer.setVisible(true);
        jButtonDevis.setVisible(true);
        jButtonInteret.setVisible(true);
    }
    // Affiche les boutons pour une demande intérêt

    private void boutonsInteret() {
        jButtonEditer.setVisible(true);
        jButtonDevis.setVisible(true);
    }
    // Affiche les boutons pour une demande devis

    private void boutonsDevis() {
        jButtonEditer.setVisible(true);
        jButtonValider.setVisible(true);
    }
    // Affiche les boutons pour une demande devis validé

    private void boutonsDevisValide() {
        jButtonImprimer.setVisible(true);
        jButtonAccepter.setVisible(true);
        jButtonRefuser.setVisible(true);
    }
    // Affiche les boutons pour une demande devis accepté = commande

    private void boutonsCommande() {
        jButtonCommande.setVisible(true);
    }

    private void addCellHeader(Table tableau, Cell cell) {
        cell.setHeader(true);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        tableau.addCell(cell);
    }

    private void addCell(Table tableau, Cell cell) {
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        tableau.addCell(cell);
    }

    private void testPDF() {
//        // Définir la taille des page
//        Rectangle pagesize = new Rectangle(216f, 720f);
//        Document document = new Document(pagesize, 36f, 72f, 108f, 180f);
//        // Création du fichier PDF
//        PdfWriter.getInstance(document, new FileOutputStream("java.pdf"));
//        // Ouvrir le fichier créé
//        document.open();
//        // Ecrire dans le fichier PDF
//        document.add(new Paragraph("MesExemples.com "+
//        	"Apprendre à programmer avec des exemples"));
//        // Fermer le document, à ne pas oublier
//        document.close();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane5 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jLabelNumDemande = new javax.swing.JLabel();
        jLabelEtatDemande = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableDetailDemande = new javax.swing.JTable();
        jButtonEditer = new javax.swing.JButton();
        jButtonInteret = new javax.swing.JButton();
        jButtonDevis = new javax.swing.JButton();
        jButtonValider = new javax.swing.JButton();
        jButtonImprimer = new javax.swing.JButton();
        jButtonAccepter = new javax.swing.JButton();
        jButtonRefuser = new javax.swing.JButton();
        jButtonCommande = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setPreferredSize(new java.awt.Dimension(550, 280));

        jLabelNumDemande.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabelNumDemande.setText("jLabel1");

        jLabelEtatDemande.setText("Etat demande :");

        jTableDetailDemande.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Article", "Référence", "Quantité", "Prix UHT"
            }
        ));
        jScrollPane1.setViewportView(jTableDetailDemande);

        jButtonEditer.setText("Editer");
        jButtonEditer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditerActionPerformed(evt);
            }
        });

        jButtonInteret.setText("Intérêt");
        jButtonInteret.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonInteretActionPerformed(evt);
            }
        });

        jButtonDevis.setText("Devis");
        jButtonDevis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDevisActionPerformed(evt);
            }
        });

        jButtonValider.setText("Valider devis");
        jButtonValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonValiderActionPerformed(evt);
            }
        });

        jButtonImprimer.setText("Imprimer");
        jButtonImprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimerActionPerformed(evt);
            }
        });

        jButtonAccepter.setText("Accepter");
        jButtonAccepter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAccepterActionPerformed(evt);
            }
        });

        jButtonRefuser.setText("Refuser");
        jButtonRefuser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRefuserActionPerformed(evt);
            }
        });

        jButtonCommande.setText("Voir Commande");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelNumDemande)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jButtonInteret)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButtonDevis)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButtonValider)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButtonImprimer)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButtonAccepter)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButtonRefuser))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabelEtatDemande)
                                .addGap(124, 124, 124)
                                .addComponent(jButtonCommande)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonEditer))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabelNumDemande)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelEtatDemande)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonEditer)
                        .addComponent(jButtonCommande)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonInteret)
                    .addComponent(jButtonDevis)
                    .addComponent(jButtonValider)
                    .addComponent(jButtonImprimer)
                    .addComponent(jButtonAccepter)
                    .addComponent(jButtonRefuser))
                .addContainerGap(99, Short.MAX_VALUE))
        );

        jScrollPane5.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonInteretActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInteretActionPerformed
        // TODO add your handling code here:
        demande.setTypeDemande(TypeDemande.INTERET);
        DemandeServices srvDem = new DemandeServices();
        srvDem.create(demande, client);
        // {{{ COR [type_demande]
        DetailDemandeServices srvDd = new DetailDemandeServices();
        for (DetailDemande dd : ListDetailDemande) {
            Article art = srvDd.getArticle(dd);
            Prix pr = srvDd.getPrix(dd);

            dd.setTypeDemande(TypeDemande.INTERET);
            srvDd.create(dd, demande);
            srvDd.linkArticle(art, dd);
            srvDd.linkPrix(pr, dd);

        }
        // COR }}}
        initData();
        ficheClient.majDemandes();
    }//GEN-LAST:event_jButtonInteretActionPerformed

    private void jButtonDevisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDevisActionPerformed
        // TODO add your handling code here:
        demande.setTypeDemande(TypeDemande.DEVIS);
        DemandeServices srvDem = new DemandeServices();
        srvDem.create(demande, client);
        // {{{ COR [type_demande]
        DetailDemandeServices srvDd = new DetailDemandeServices();
        for (DetailDemande dd : ListDetailDemande) {
            Article art = srvDd.getArticle(dd);
            Prix pr = srvDd.getPrix(dd);

            dd.setTypeDemande(TypeDemande.DEVIS);
            srvDd.create(dd, demande);
            srvDd.linkArticle(art, dd);
            srvDd.linkPrix(pr, dd);

        }
        // COR }}}
        initData();
        ficheClient.majDemandes();
    }//GEN-LAST:event_jButtonDevisActionPerformed

    private void jButtonValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonValiderActionPerformed
        // TODO add your handling code here:
        demande.setValideDemande(1);
        DemandeServices srvDem = new DemandeServices();
        srvDem.update(demande);
        initData();
        ficheClient.majDemandes();
    }//GEN-LAST:event_jButtonValiderActionPerformed

    private void jButtonAccepterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAccepterActionPerformed
        // TODO add your handling code here:

        DemandeServices srvDem = new DemandeServices();
        CommandeServices srvCom = new CommandeServices();
        DetailCommandeServices srvDc = new DetailCommandeServices();
        DetailDemandeServices srvDd = new DetailDemandeServices();

        Commande commande = new Commande();
//        Article article = new Article();
//        Prix prix = new Prix();
        DetailCommande detailCommande = new DetailCommande();
//        List<DetailDemande> detdem = srvDd.get(demande.getIdAppli());

        demande.setTypeDemande(TypeDemande.COMMANDE);
        srvDem.create(demande, client);

        commande.setIdAdresseFacturation(demande.getIdAdresseFacturation());
        commande.setIdAdresseLivraison(demande.getIdAdresseLivraison());
        commande.setNumCommande(demande.getNumDemande());
        commande.setEtatCommande(EtatCommande.ENCOURS);
        commande.setDate(new DateTime().toDate());
        srvCom.create(commande, client);

//        for (int i = 0; i < jTableDetailDemande.getRowCount(); i++) {
        for (DetailDemande det : ListDetailDemande) {
            if (det.getTypeDemande() == TypeDemande.DEVIS) {
//                article = (Article) jTableDetailDemande.getValueAt(i, 0);
//                prix = (Prix) jTableDetailDemande.getValueAt(i, 3);
//                detailCommande.setQuantite(Integer.valueOf(jTableDetailDemande.getValueAt(i, 2).toString()).intValue());
                Article art = srvDd.getArticle(det);
                Prix prix = srvDd.getPrix(det);
                detailCommande.setQuantite(det.getQuantite());

                det.setTypeDemande(TypeDemande.COMMANDE);
                srvDd.create(det, demande);
                srvDd.linkArticle(art, det);
                srvDd.linkPrix(prix, det);

                srvDc.create(detailCommande, commande);
                srvDc.linkArticle(art, detailCommande);
                srvDc.linkPrix(prix, detailCommande);
            }
        }

        initData();
        ficheClient.majDemandes();
        ficheClient.majCommandes();
        ficheClient.getJTableDemandes().clearSelection();
    }//GEN-LAST:event_jButtonAccepterActionPerformed

    private void jButtonRefuserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRefuserActionPerformed
        // TODO add your handling code here:
        demande.setValideDemande(3);
        DemandeServices srvDem = new DemandeServices();
        srvDem.update(demande);
        initData();
        ficheClient.majDemandes();
    }//GEN-LAST:event_jButtonRefuserActionPerformed

    private void jButtonEditerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditerActionPerformed
        // TODO add your handling code here:
        FormulaireDemande fd = new FormulaireDemande(ficheClient, client, demande);
        fd.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButtonEditerActionPerformed

    private void jButtonImprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimerActionPerformed
        // TODO add your handling code here:
        Document document = new Document(PageSize.A4);
        try {
            double prixTotal = 0;
            PdfWriter.getInstance(document,
                    new FileOutputStream("c:/temp/test.pdf"));
            document.open();
            Table tableau = new Table(5, 2);
            tableau.setAutoFillEmptyCells(true);
            tableau.setPadding(2);

            Cell cell = new Cell("Article");
            addCellHeader(tableau, cell);
            cell = new Cell("Référence");
            addCellHeader(tableau, cell);
            cell = new Cell("Quantité");
            addCellHeader(tableau, cell);
            cell = new Cell("Prix UHT");
            addCellHeader(tableau, cell);
            cell = new Cell("Prix total");
            addCellHeader(tableau, cell);
            tableau.endHeaders();

            List<DetailDemande> detailDemandes = new ArrayList<DetailDemande>();

            DemandeServices srvDem = new DemandeServices();
            DetailDemandeServices srvDet = new DetailDemandeServices();
            detailDemandes = srvDem.getDetailDemandes(demande);
            final DefaultTableModel model = (DefaultTableModel) jTableDetailDemande.getModel();
            model.getDataVector().removeAllElements();

            double prixArticles = 0;

            for (DetailDemande detailDemande : detailDemandes) {
//            System.out.print(demande.getNumDemande());
                Article article = srvDet.getArticle(detailDemande);
                Prix prix = srvDet.getPrix(detailDemande);
                cell = new Cell(article.getLibelle());
                addCell(tableau, cell);
                cell = new Cell(article.getReference());
                addCell(tableau, cell);
                cell = new Cell(String.valueOf(detailDemande.getQuantite()).toString());
                addCell(tableau, cell);
                cell = new Cell(String.valueOf(prix.getPrixUht()).toString());
                addCell(tableau, cell);
                prixArticles = prix.getPrixUht() * detailDemande.getQuantite();
                prixArticles = ((double) Math.round(prixArticles * 100) / 100.0);
                cell = new Cell(String.valueOf(prixArticles).toString());
                addCell(tableau, cell);
                prixTotal = prixTotal + prixArticles;
            }
            Paragraph paragraph = new Paragraph("Devis n° " + demande.getNumDemande(), new Font(Font.COURIER, 24, Font.BOLD, Color.black));
            paragraph.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph);
            paragraph.setLeading(30f);
            document.add(new Paragraph("Date :" + new DateTime(demande.getDate()).toString("dd/MM/YYYY")));

            paragraph = new Paragraph("PlastProd");
            paragraph.setLeading(30f);
            document.add(paragraph);
            document.add(new Paragraph("Zone d'activité Nord"));
            document.add(new Paragraph("76000 Rouen"));

            AdresseServices srvAdr = new AdresseServices();
            Adresse adresse = srvAdr.get(demande.getIdAdresseFacturation());

            paragraph = new Paragraph(client.getNom());
            paragraph.setAlignment(Element.ALIGN_RIGHT);
            paragraph.setLeading(30f);
            document.add(paragraph);

            paragraph = new Paragraph(adresse.getNumVoie() + " " + adresse.getNomVoie());
            paragraph.setAlignment(Element.ALIGN_RIGHT);
            document.add(paragraph);

            if (null != adresse.getComplement()) {
                paragraph = new Paragraph(adresse.getComplement());
                paragraph.setAlignment(Element.ALIGN_RIGHT);
                document.add(paragraph);
            }
            paragraph = new Paragraph(adresse.getCodePostal() + " " + adresse.getVille());
            paragraph.setAlignment(Element.ALIGN_RIGHT);
            document.add(paragraph);

            paragraph = new Paragraph(adresse.getPays());
            paragraph.setAlignment(Element.ALIGN_RIGHT);
            document.add(paragraph);

            document.add(tableau);
            paragraph = new Paragraph("Prix total : " + ((double) Math.round(prixTotal * 100) / 100));
            paragraph.setAlignment(Element.ALIGN_RIGHT);
            document.add(paragraph);
        } catch (DocumentException de) {
            de.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        document.close();
        try {
            Desktop.getDesktop().open(new File("c:/temp/test.pdf"));
        } catch (IOException ex) {
            Logger.getLogger(FicheDetailDemande.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButtonImprimerActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAccepter;
    private javax.swing.JButton jButtonCommande;
    private javax.swing.JButton jButtonDevis;
    private javax.swing.JButton jButtonEditer;
    private javax.swing.JButton jButtonImprimer;
    private javax.swing.JButton jButtonInteret;
    private javax.swing.JButton jButtonRefuser;
    private javax.swing.JButton jButtonValider;
    private javax.swing.JLabel jLabelEtatDemande;
    private javax.swing.JLabel jLabelNumDemande;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTableDetailDemande;
    // End of variables declaration//GEN-END:variables
}
